package com.song.shop.transact;

import com.song.shop.mongodb.Product;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * @author Song Yinghui
 * @date 2019/12/4
 * @Version 1.0
 */
public class Transaction  {

    private TransactionTemplate transactionTemplate;

    public void test(){
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {

            }
        });

        transactionTemplate.execute(new TransactionCallback<Product>() {
            /**
             * Gets called by {@link TransactionTemplate#execute} within a transactional context.
             * Does not need to care about transactions itself, although it can retrieve and
             * influence the status of the current transaction via the given status object,
             * e.g. setting rollback-only.
             * <p>Allows for returning a result object created within the transaction, i.e. a
             * domain object or a collection of domain objects. A RuntimeException thrown by the
             * callback is treated as application exception that enforces a rollback. Any such
             * exception will be propagated to the caller of the template, unless there is a
             * problem rolling back, in which case a TransactionException will be thrown.
             *
             * @param status associated transaction status
             * @return a result object, or {@code null}
             * @see TransactionTemplate#execute
             * @see CallbackPreferringPlatformTransactionManager#execute
             */
            @Override
            public Product doInTransaction(TransactionStatus status) {
                return null;
            }
        });
    }

}
